package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: feisl
 * @Date: 2021/5/24 15:29
 */
@RestController
@RequestMapping("/shopping")
public class ProductController {

   @Reference(timeout = 30000,retries = 0,check = false)
   IProductService iProductService;



   @GetMapping("product/{id}")
   @Anoymous
    public ResponseData goods(@PathVariable("id") Long id){

       ProductDetailRequest productDetailRequest = new ProductDetailRequest();
       productDetailRequest.setId(id);
       ProductDetailResponse productDetailResponse = iProductService.getProductDetail(productDetailRequest);

       if(productDetailResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
           return new ResponseUtil<>().setData(productDetailResponse.getProductDetailDto());
       }

       return new ResponseUtil<>().setErrorMsg(productDetailResponse.getMsg());
   }

   @ApiOperation("分页查询")
   @GetMapping("goods")
   public ResponseData GoodsFind(AllProductRequest allProductRequest){
      AllProductResponse allProduct = iProductService.getAllProduct(allProductRequest);
      goods goods = new goods();
      goods.setData(allProduct.getProductDtoList());
      goods.setTotal(allProduct.getTotal());

      return new ResponseUtil().setData(goods);
   }

   @Data
   private class goods{
      List<ProductDto> data;
      Long total;
   }

   @Anoymous
   @GetMapping("/recommend")
   public ResponseData recommend() {
      RecommendResponse response = iProductService.getRecommendGoods();
      if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
         return new ResponseUtil().setData(response.getPanelContentItemDtos());
      }
      return new ResponseUtil().setErrorMsg(response.getMsg());
   }
}
